<template>
  <div>
    <el-form label-width="80px">
      <el-form-item label="Spu名称"> {{ sku.spuName }} </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          type="number"
          v-model="skuInfo.price"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input
          placeholder="重量(千克)"
          v-model="skuInfo.weight"
          type="number"
        ></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          :autosize="{ minRows: 4 }"
          placeholder="规格描述"
          v-model="skuInfo.skuDesc"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="平台属性" label-width="80px">
        <el-form :inline="true">
          <el-form-item
            :label="attr.attrName"
            v-for="(attr, index) in attrInfoList"
            :key="attr.id"
          >
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option
                :label="value.valueName"
                :value="`${attr.id}:${value.id}`"
                v-for="(value, index) in attr.attrValueList"
                :key="value.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性" label-width="80px">
        <el-form :inline="true">
          <el-form-item
            :label="item.saleAttrName"
            v-for="(item, index) in spuSaleAttrList"
            :key="item.id"
          >
            <el-select placeholder="请选择" v-model="item.attrIdAndchiId">
              <el-option
                :label="chi.saleAttrValueName"
                :value="`${item.id}:${chi.id}`"
                v-for="(chi, index) in item.spuSaleAttrValueList"
                :key="chi.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          border
          :data="spuImageList"
          @selection-change="handleSelectionChange"
          >>
          <el-table-column width="80px" type="selection"></el-table-column>
          <el-table-column label="图片">
            <template slot-scope="{ row, $index }">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row, $index }">
              <el-button
                type="primary"
                size="default"
                v-if="row.isDefault == 0"
                @click="changeisDefault(row)"
                >设置默认</el-button
              >
              <el-button v-else size="default">默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuFrom",
  data() {
    return {
      spuImageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      skuInfo: {
        //第一类收集的数据：父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类：需要通过数据双向绑定v-model收集
        skuName: "",
        price: 0,
        weight: "",
        skuDesc: "",
        //第三类：需要自己书写代码
        //默认图片
        skuDefaultImg: "",
        //收集图片的字段
        skuImageList: [
          // {
          //   id: 0,
          //   imgName: "string",
          //   imgUrl: "string",
          //   isDefault: "string",
          //   skuId: 0,
          //   spuImgId: 0,
          // },
        ],
        //平台属性
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   valueId: 0,
          // },
        ],
        //销售属性
        skuSaleAttrValueList: [
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: "string",
          //   saleAttrValueId: 0,
          //   saleAttrValueName: "string",
          //   skuId: 0,
          //   spuId: 0,
          // },
        ],
      },
      sku: {},
      imageList: [],
    };
  },
  methods: {
    async getData(category1Id, category2Id, spu) {
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      this.sku = spu;
      let result = await this.$API.spu.reqSkuImageList(spu.id);
      if (result.code == 200) {
        let list = result.data;
        list.forEach((item) => {
          item.isDefault = 0;
        });
        this.spuImageList = list;
      }

      //获取销售属性
      let result1 = await this.$API.spu.reqSpusaleAttrList(spu.id);
      if (result1.code == 200) {
        this.spuSaleAttrList = result1.data;
      }

      //获取平台属性
      let result2 = await this.$API.spu.reqAttrInfoList(
        category1Id,
        category2Id,
        spu.category3Id
      );
      if (result1.code == 200) {
        this.attrInfoList = result2.data;
      }
    },
    handleSelectionChange(params) {
      this.imageList = params;
    },
    //排他
    changeisDefault(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
      });
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    cancel() {
      this.$emit("changgeScenes", 0);
      Object.assign(this._data, this.$options.data());
    },
    //保存按钮
    async save() {
      //平台属性
      const { attrInfoList, skuInfo, spuSaleAttrList, imageList } = this;
      let arr = [];
      attrInfoList.forEach((item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(":");
          let obj = { valueId, attrId };
          arr.push(obj);
        }
      });
      skuInfo.skuAttrValueList = arr;

      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndchiId) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndchiId.split(":");
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);
      skuInfo.skuImageList = imageList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });
     let result = await this.$API.spu.reqAddSku(skuInfo)
     if(result.code == 200){
       this.$message({type:'success',message:'保存成功'})
       this.$emit('changgeScenes',0)
     }
    },
  },
};
</script>

<style>
</style>